﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;

namespace jaam_butters.Agents
{
    public class AnimatedSprite
    {
        Texture2D spriteTexture;
        float timer = 0f;
        float interval = 200f;
        int currentFrame = 0;
        int spriteWidth = 31;
        int spriteHeight = 32;

        Rectangle sourceRect;
        Vector2 position;
        Vector2 origin;

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Vector2 Origin
        {
            get { return origin; }
            set { origin = value; }
        }


        public Texture2D Texture
        {
            get { return spriteTexture; }
            set { spriteTexture = value; }
        }


        public Rectangle SourceRect
        {
            get { return sourceRect; }
            set { sourceRect = value; }
        }

        public AnimatedSprite(Texture2D texture, int currentFrame, int spriteWidth, int spriteHeight)
        {
            this.spriteTexture = texture;
            this.currentFrame = currentFrame;
            this.spriteWidth = spriteWidth;
            this.spriteHeight = spriteHeight;
            this.origin = new Vector2(spriteWidth / 2, spriteHeight / 2);
            this.sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
        }

        public void HandleSpriteMovement(GameTime gameTime)
        {
            sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
            /*
            if (currentFrame > 0 && currentFrame < 2)
            {
                currentFrame = 0;
            }

            if (currentFrame > 3 && currentFrame < 5)
            {
                currentFrame = 3;
            }

            if (currentFrame > 6 && currentFrame < 8)
            {
                currentFrame = 6;
            }

            if (currentFrame > 9 && currentFrame < 11)
            {
                currentFrame = 9;
            }*/

        }


        public void AnimateRight(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                currentFrame++;

                if (currentFrame > 8 || currentFrame<6)
                {
                    currentFrame = 6;
                }
                timer = 0f;
            }
        }


        public void AnimateUp(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                currentFrame++;

                if (currentFrame > 11 || currentFrame < 9)
                {
                    currentFrame = 9;
                }
                timer = 0f;
            }
        }

        public void AnimateDown(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                currentFrame++;

                if (currentFrame > 2)
                {
                    currentFrame = 0;
                }
                timer = 0f;
            }
        }

        public void AnimateLeft(GameTime gameTime)
        {
            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                currentFrame++;

                if (currentFrame > 5 || currentFrame < 3)
                {
                    currentFrame = 3;
                }
                timer = 0f;
            }
        }

    }
}

